{extend name="common@base/common" /}

{block name="style"}
<link href="__CSS__/main.min.css" rel="stylesheet" type="text/css"/>
<style>
.btn.drag-shadow {
  z-index: 9999; /* 设置影子元素的 z-index，保证在页面顶层显示 */
}
#droppableBtn {
  cursor: move; /* 为被拖动的按钮更改光标类型 */
}
#droppableContainer .area-name {
  font-size: 50px;
  line-height: 80px;
  text-align: center;
  color: #808080;
}
</style>


<style>
#multiDroppableContainer .btn-droppable {
  cursor: move; /* 为被拖动的按钮更改光标类型 */
}
#multiDroppableContainer .area-name {
  font-size: 50px;
  line-height: 80px;
  text-align: center;
  color: #808080;
}
</style>
{/block}

{block name="body"}
<div class="container">
  <ol class="breadcrumb">
      <li><a href="{:url('index/index')}">演示首页</a></li>
      <li>拖放</li>
  </ol>
<section class="demo-section">
<div id="pageContent">

<article>
    <p>拖放插件帮助你判断用户是否将元素拖动并放置在目标容器上。</p>
    <p>拖动时会在文档中插入被拖动元素的<strong>影子元素</strong>，会跟随鼠标光标位置，用于指示当前拖动的位置。</p>
    <p>拖放插件不会更改你的元素 DOM 结构，也就是说如果你需要将用户拖动的元素移动到目标容器内，则需要你自己使用 JavaScript 来移动相关 DOM 节点。</p>
    <div class="alert alert-danger">
      <h4>兼容性问题</h4>
      <p>在触摸屏上无法支持拖拽功能。</p>
    </div>
</article>

<section>
    <header>
        <h3>示例</h3></header>
    <article>
        <h4>简单应用</h4>
    <div class="example">
<div class="row" id="droppableContainer">
  <div class="col-sm-3">
    <div class="panel">
      <div class="panel-heading">开始吧</div>
      <div class="panel-body" style="height: 120px; text-align: center; line-height: 80px">
        <button type="button" class="btn btn-danger btn-lg" id="droppableBtn"><i class="icon icon-move"></i> 拖动我</button>
      </div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="panel droppable-target">
      <div class="panel-heading">拖动到这里。</div>
      <div class="panel-body" style="height: 120px">
        <div class="area-name">A</div>
      </div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="panel droppable-target">
      <div class="panel-heading">这里。</div>
      <div class="panel-body" style="height: 120px">
        <div class="area-name">B</div>
      </div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="panel droppable-target">
      <div class="panel-heading">或这里。</div>
      <div class="panel-body" style="height: 120px">
        <div class="area-name">C</div>
      </div>
    </div>
  </div>
</div>
    </div>

<pre class="pre-scrollable prettyprint prettyprinted" style=""><code class="lang-html" id="code-fhcetbtlsq"><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"row"</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"droppableContainer"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"col-sm-3"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-heading"</span><span class="tag">&gt;</span><span class="pln">开始吧</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-body"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">120px</span><span class="pun">;</span><span class="pln"> text</span><span class="pun">-</span><span class="pln">align</span><span class="pun">:</span><span class="pln"> center</span><span class="pun">;</span><span class="pln"> line</span><span class="pun">-</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">80px</span><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">
        </span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-danger btn-lg"</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"droppableBtn"</span><span class="tag">&gt;&lt;i</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"icon icon-move"</span><span class="tag">&gt;&lt;/i&gt;</span><span class="pln"> 拖动我</span><span class="tag">&lt;/button&gt;</span><span class="pln">
      </span><span class="tag">&lt;/div&gt;</span><span class="pln">
    </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"col-sm-3"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel droppable-target"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-heading"</span><span class="tag">&gt;</span><span class="pln">拖动到这里。</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-body"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">120px</span><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">
        </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"area-name"</span><span class="tag">&gt;</span><span class="pln">A</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;/div&gt;</span><span class="pln">
    </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"col-sm-3"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel droppable-target"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-heading"</span><span class="tag">&gt;</span><span class="pln">这里。</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-body"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">120px</span><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">
        </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"area-name"</span><span class="tag">&gt;</span><span class="pln">B</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;/div&gt;</span><span class="pln">
    </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"col-sm-3"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel droppable-target"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-heading"</span><span class="tag">&gt;</span><span class="pln">或这里。</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-body"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">120px</span><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">
        </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"area-name"</span><span class="tag">&gt;</span><span class="pln">C</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;/div&gt;</span><span class="pln">
    </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;/div&gt;</span><span class="pln">
</span><span class="tag">&lt;/div&gt;</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-css"><span class="com">/* 设置影子元素的 z-index，保证在页面顶层显示 */</span><span class="pln">
</span><span class="pun">.</span><span class="pln">btn</span><span class="pun">.</span><span class="pln">drag</span><span class="pun">-</span><span class="pln">shadow </span><span class="pun">{</span><span class="pln"> z</span><span class="pun">-</span><span class="pln">index</span><span class="pun">:</span><span class="pln"> </span><span class="lit">9999</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

</span><span class="com">/* 为被拖动的按钮更改光标类型 */</span><span class="pln">
</span><span class="com">#droppableBtn { cursor: move; }</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">(</span><span class="str">'#droppableBtn'</span><span class="pun">).</span><span class="pln">droppable</span><span class="pun">({</span><span class="pln">
    target</span><span class="pun">:</span><span class="pln"> </span><span class="str">'.droppable-target'</span><span class="pun">,</span><span class="pln">
    start</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $</span><span class="pun">(</span><span class="str">'#droppableContainer .droppable-target'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-warning'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-success'</span><span class="pun">).</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.panel-heading'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">(</span><span class="str">'拖动到这里吗？'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">},</span><span class="pln">
    drop</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">var</span><span class="pln"> msg </span><span class="pun">=</span><span class="pln"> </span><span class="str">'真棒！'</span><span class="pun">;</span><span class="pln">
        $</span><span class="pun">(</span><span class="str">'#droppableContainer .droppable-target'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-success'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-warning'</span><span class="pun">);</span><span class="pln">
        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">event</span><span class="pun">.</span><span class="pln">target</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            event</span><span class="pun">.</span><span class="pln">target</span><span class="pun">.</span><span class="pln">addClass</span><span class="pun">(</span><span class="str">'panel-success'</span><span class="pun">).</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.panel-heading'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">(</span><span class="str">'成功拖到目的地。'</span><span class="pun">);</span><span class="pln">
            msg </span><span class="pun">+=</span><span class="pln"> </span><span class="str">'成功拖动到区域 '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> event</span><span class="pun">.</span><span class="pln">target</span><span class="pun">.</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.area-name'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">();</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">
        $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="pln">messager</span><span class="pun">.</span><span class="pln">show</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">},</span><span class="pln">
    drag</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $</span><span class="pun">(</span><span class="str">'#droppableContainer .droppable-target'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-success'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-warning'</span><span class="pun">);</span><span class="pln">
        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">event</span><span class="pun">.</span><span class="pln">target</span><span class="pun">)</span><span class="pln"> event</span><span class="pun">.</span><span class="pln">target</span><span class="pun">.</span><span class="pln">addClass</span><span class="pun">(</span><span class="str">'panel-warning'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">});</span></code></pre><h4>拖放容器内的多个元素</h4>


<div class="example">
  <div class="row" id="multiDroppableContainer">
    <div class="col-sm-3">
      <div class="panel">
        <div class="panel-heading">开始吧</div>
        <div class="panel-body" style="height: 150px; text-align: center; line-height: 40px">
          <button type="button" class="btn btn-primary btn-droppable"><i class="icon icon-move"></i> 按钮 #<span class="btn-droppable-id">1</span></button><br>
          <button type="button" class="btn btn-success btn-droppable"><i class="icon icon-move"></i> 按钮 #<span class="btn-droppable-id">2</span></button><br>
          <button type="button" class="btn btn-danger btn-droppable"><i class="icon icon-move"></i> 按钮 #<span class="btn-droppable-id">3</span></button>
        </div>
      </div>
    </div>
    <div class="col-sm-3">
      <div class="panel droppable-target" data-id="1">
        <div class="panel-heading">拖动到这里。</div>
        <div class="panel-body" style="height: 150px">
          <div class="area-name">A</div>
        </div>
      </div>
    </div>
    <div class="col-sm-3">
      <div class="panel droppable-target" data-id="2">
        <div class="panel-heading">这里。</div>
        <div class="panel-body" style="height: 150px">
          <div class="area-name">B</div>
        </div>
      </div>
    </div>
    <div class="col-sm-3">
      <div class="panel droppable-target" data-id="3">
        <div class="panel-heading">或这里。</div>
        <div class="panel-body" style="height: 150px">
          <div class="area-name">C</div>
        </div>
      </div>
    </div>
  </div>
</div>

<pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"row"</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"multiDroppableContainer"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"col-sm-3"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-heading"</span><span class="tag">&gt;</span><span class="pln">开始吧</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-body"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">150px</span><span class="pun">;</span><span class="pln"> text</span><span class="pun">-</span><span class="pln">align</span><span class="pun">:</span><span class="pln"> center</span><span class="pun">;</span><span class="pln"> line</span><span class="pun">-</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">40px</span><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">
        </span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary btn-droppable"</span><span class="tag">&gt;&lt;i</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"icon icon-move"</span><span class="tag">&gt;&lt;/i&gt;</span><span class="pln"> 按钮 #</span><span class="tag">&lt;span</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn-droppable-id"</span><span class="tag">&gt;</span><span class="pln">1</span><span class="tag">&lt;/span&gt;&lt;/button&gt;&lt;br&gt;</span><span class="pln">
        </span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-success btn-droppable"</span><span class="tag">&gt;&lt;i</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"icon icon-move"</span><span class="tag">&gt;&lt;/i&gt;</span><span class="pln"> 按钮 #</span><span class="tag">&lt;span</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn-droppable-id"</span><span class="tag">&gt;</span><span class="pln">2</span><span class="tag">&lt;/span&gt;&lt;/button&gt;&lt;br&gt;</span><span class="pln">
        </span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-danger btn-droppable"</span><span class="tag">&gt;&lt;i</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"icon icon-move"</span><span class="tag">&gt;&lt;/i&gt;</span><span class="pln"> 按钮 #</span><span class="tag">&lt;span</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn-droppable-id"</span><span class="tag">&gt;</span><span class="pln">3</span><span class="tag">&lt;/span&gt;&lt;/button&gt;</span><span class="pln">
      </span><span class="tag">&lt;/div&gt;</span><span class="pln">
    </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"col-sm-3"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel droppable-target"</span><span class="pln"> </span><span class="atn">data-id</span><span class="pun">=</span><span class="atv">"1"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-heading"</span><span class="tag">&gt;</span><span class="pln">拖动到这里。</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-body"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">150px</span><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">
        </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"area-name"</span><span class="tag">&gt;</span><span class="pln">A</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;/div&gt;</span><span class="pln">
    </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"col-sm-3"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel droppable-target"</span><span class="pln"> </span><span class="atn">data-id</span><span class="pun">=</span><span class="atv">"2"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-heading"</span><span class="tag">&gt;</span><span class="pln">这里。</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-body"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">150px</span><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">
        </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"area-name"</span><span class="tag">&gt;</span><span class="pln">B</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;/div&gt;</span><span class="pln">
    </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"col-sm-3"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel droppable-target"</span><span class="pln"> </span><span class="atn">data-id</span><span class="pun">=</span><span class="atv">"3"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-heading"</span><span class="tag">&gt;</span><span class="pln">或这里。</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"panel-body"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">150px</span><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">
        </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"area-name"</span><span class="tag">&gt;</span><span class="pln">C</span><span class="tag">&lt;/div&gt;</span><span class="pln">
      </span><span class="tag">&lt;/div&gt;</span><span class="pln">
    </span><span class="tag">&lt;/div&gt;</span><span class="pln">
  </span><span class="tag">&lt;/div&gt;</span><span class="pln">
</span><span class="tag">&lt;/div&gt;</span></code></pre>

<pre class="prettyprint prettyprinted" style=""><code class="lang-css"><span class="com">#multiDroppableContainer .btn-droppable {</span><span class="pln">
  cursor</span><span class="pun">:</span><span class="pln"> move</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* 为被拖动的按钮更改光标类型 */</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="com">#multiDroppableContainer .area-name {</span><span class="pln">
  font</span><span class="pun">-</span><span class="pln">size</span><span class="pun">:</span><span class="pln"> </span><span class="lit">50px</span><span class="pun">;</span><span class="pln">
  line</span><span class="pun">-</span><span class="pln">height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">80px</span><span class="pun">;</span><span class="pln">
  text</span><span class="pun">-</span><span class="pln">align</span><span class="pun">:</span><span class="pln"> center</span><span class="pun">;</span><span class="pln">
  color</span><span class="pun">:</span><span class="pln"> </span><span class="com">#808080;</span><span class="pln">
</span><span class="pun">}</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">(</span><span class="str">'#multiDroppableContainer'</span><span class="pun">).</span><span class="pln">droppable</span><span class="pun">({</span><span class="pln">
    selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'.btn-droppable'</span><span class="pun">,</span><span class="pln"> </span><span class="com">// 定义允许拖放的元素</span><span class="pln">
    target</span><span class="pun">:</span><span class="pln"> </span><span class="str">'.droppable-target'</span><span class="pun">,</span><span class="pln">
    start</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $</span><span class="pun">(</span><span class="str">'#multiDroppableContainer .droppable-target'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-warning'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-success'</span><span class="pun">).</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.panel-heading'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">(</span><span class="str">'拖动到这里吗？'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">},</span><span class="pln">
    drop</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">var</span><span class="pln"> msg </span><span class="pun">=</span><span class="pln"> </span><span class="str">'真棒！'</span><span class="pun">;</span><span class="pln">
        $</span><span class="pun">(</span><span class="str">'#multiDroppableContainer .droppable-target'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-success'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-warning'</span><span class="pun">);</span><span class="pln">
        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">event</span><span class="pun">.</span><span class="pln">target</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            </span><span class="kwd">var</span><span class="pln"> elementId </span><span class="pun">=</span><span class="pln"> event</span><span class="pun">.</span><span class="pln">element</span><span class="pun">.</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.btn-droppable-id'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">();</span><span class="pln">
            event</span><span class="pun">.</span><span class="pln">target</span><span class="pun">.</span><span class="pln">addClass</span><span class="pun">(</span><span class="str">'panel-success'</span><span class="pun">).</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.panel-heading'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">(</span><span class="str">'成功将【按钮#'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> elementId </span><span class="pun">+</span><span class="pln"> </span><span class="str">'】拖到目的地。'</span><span class="pun">);</span><span class="pln">
            msg </span><span class="pun">+=</span><span class="pln"> </span><span class="str">'成功拖动【按钮#'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> elementId </span><span class="pun">+</span><span class="pln"> </span><span class="str">'】到区域 '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> event</span><span class="pun">.</span><span class="pln">target</span><span class="pun">.</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.area-name'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">();</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">
        $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="pln">messager</span><span class="pun">.</span><span class="pln">show</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">},</span><span class="pln">
    drag</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $</span><span class="pun">(</span><span class="str">'#multiDroppableContainer .droppable-target'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-success'</span><span class="pun">).</span><span class="pln">removeClass</span><span class="pun">(</span><span class="str">'panel-warning'</span><span class="pun">);</span><span class="pln">
        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">event</span><span class="pun">.</span><span class="pln">target</span><span class="pun">)</span><span class="pln"> event</span><span class="pun">.</span><span class="pln">target</span><span class="pun">.</span><span class="pln">addClass</span><span class="pun">(</span><span class="str">'panel-warning'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">});</span></code>
</pre>
</article>
</section>





<section>
    <header><h3>选项</h3></header>
    <article>
        <p>在进行初始化时允许传入一个对象参数作为初始化选项。可以使用的选项如下：</p>
        <div class="table-responsive">
            <table class="table table-bordered">
              <thead>
                <tr>
                  <th>选项</th>
                  <th>名称</th>
                  <th>可选值</th>
                  <th>描述</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td><code>container</code></td>
                  <td>位置计算容器</td>
                  <td>默认 <code>'body'</code></td>
                  <td>使用 jQuery 选择器来指定用于计算位置的父级容器。用于父级容器的节点其 <code>position</code> 应该为 <code>'relative'</code>、<code>'absolute'</code>及<code>'fixed'</code> 中的一种；如果为 <code>'static'</code>，则会在拖动过程中被设置为 <code>'relative'</code>。</td>
                </tr>
                <tr>
                  <td><code>selector</code></td>
                  <td>拖动元素选择器 (1.6+)</td>
                  <td>默认 <code>""</code>（可选）</td>
                  <td>当设置此选项后将 <code>$()</code> 选定的元素作为容器，<code>selector</code> 选项用于指定容器内的哪些元素可以拖动。</td>
                </tr>
                <tr>
                  <td><code>handle</code></td>
                  <td>拖动事件触发元素选择器</td>
                  <td>默认为 <code>null</code></td>
                  <td>用于选择被拖动元素内部元素的选择器，如果指定该选项，则仅当鼠标在指定的子元素上点按鼠标会触发拖动事件。</td>
                </tr>
                <tr>
                  <td><code>target</code></td>
                  <td>定义可放置元素</td>
                  <td>必选项，默认 <code>'.droppable-target'</code></td>
                  <td>值类型为 jQuery 实例或有效的 jQuery 选择器字符串，也可以设置为一个回调函数来返回可放置元素。</td>
                </tr>
                <tr>
                  <td><code>mouseButton</code></td>
                  <td>响应的鼠标按钮</td>
                  <td>默认为 <code>'all'</code></td>
                  <td>
                    <p>所有可响应的鼠标按钮参考 <a href="https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/button" target="_blank"><code>MouseEvent.button</code></a>：</p>
                    <ul>
                      <li><code>'all'</code> 或 <code>-1</code>：响应所有鼠标按键；</li>
                      <li><code>'left'</code> 或 <code>0</code>：响应鼠标左键；</li>
                      <li><code>'middle'</code> 或 <code>1</code>：响应鼠标滚轮（中键）；</li>
                      <li><code>'right'</code> 或 <code>2</code>：响应鼠标右键。</li>
                    </ul>
                  </td>
                </tr>
                <tr>
                  <td><code>flex</code></td>
                  <td><code>true</code>、<code>false</code>（默认）</td>
                  <td>是否执行宽松判断</td>
                  <td>如果设置为 <code>true</code>，当被拖动元素离开其中一个目标放置元素但没有进入下一个目标放置元素之前，仍然认定是放置在离开前的目标放置元素之上。</td>
                </tr>
                <tr>
                  <td><code>deviation</code></td>
                  <td>最小移动有效距离</td>
                  <td>默认为 <code>5</code></td>
                  <td>当鼠标移动的距离在水平方向和垂直方向均小于此值时会忽略拖动判断来提高运行性能。</td>
                </tr>
                <tr>
                  <td><code>sensorOffsetX</code></td>
                  <td>水平方向上的额外判断距离</td>
                  <td>默认为 <code>0</code></td>
                  <td>如果设定了此值，即使当前光标没有进入下一个目标放置元素但在水平方向上小于此值时仍然认定放置有效，增大此值可以提高判断敏感性。</td>
                </tr>
                <tr>
                  <td><code>sensorOffsetY</code></td>
                  <td>垂直方向上的额外判断距离</td>
                  <td>默认为 <code>0</code></td>
                  <td>如果设定了此值，即使当前光标没有进入下一个目标放置元素但在垂直方向上小于此值时仍然认定放置有效，增大此值可以提高判断敏感性。</td>
                </tr>
                <tr>
                  <td><code>before</code></td>
                  <td>拖动前回调函数</td>
                  <td>默认不设置</td>
                  <td>指定一个回调函数在拖动发生前（<code>mousedown</code> 事件发生时）调用，在此回调函数中返回 <code>false</code> 来取消此次拖动操作。</td>
                </tr>
                <tr>
                  <td><code>start</code></td>
                  <td>拖动开始时的回调函数</td>
                  <td>默认不设置</td>
                  <td>指定一个回调函数在拖动发生前（第一次 <code>mousemove</code> 事件发生时）调用并且已经开始拖动时。</td>
                </tr>
                <tr>
                  <td><code>drag</code></td>
                  <td>移动时回调函数</td>
                  <td>默认不设置</td>
                  <td>指定一个回调函数在移动目标元素时前（<code>mousemove</code> 事件发生时）调用。</td>
                </tr>
                <tr>
                  <td><code>beforeDrop</code></td>
                  <td>决定为有效放置的回调函数</td>
                  <td>默认不设置</td>
                  <td>在函数内返回 <code>true</code> 或 <code>false</code> 来决定是否为有效的放置。</td>
                </tr>
                <tr>
                  <td><code>drop</code></td>
                  <td>拖动并有效放置的回调函数</td>
                  <td>默认不设置</td>
                  <td>指定一个回调函数在完成移动目标元素时前（<code>mouseup</code> 事件发生时）并且已经将目标元素拖动到有效的可放置元素上时调用。</td>
                </tr>
                <tr>
                  <td><code>finish</code></td>
                  <td>拖动完成回调函数</td>
                  <td>默认不设置</td>
                  <td>指定一个回调函数在拖动完成后（<code>mouseup</code> 事件发生时并且已经发生过移动事件 <code>mousemove</code>）调用。</td>
                </tr>
                <tr>
                  <td><code>always</code></td>
                  <td>操作结束时的回调函数</td>
                  <td>默认不设置</td>
                  <td>指定一个回调函数在操作完成后（<code>mouseup</code> 事件发生时，即使没有发生 <code>mousemove</code> 事件）调用。</td>
                </tr>
              </tbody>
            </table>
        </div>
<p>使用选项：</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">// 定义选项对象</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> options </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    target</span><span class="pun">:</span><span class="pln"> </span><span class="str">'.droppable-target'</span><span class="pun">,</span><span class="pln">
    drop</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'成功拖到目标：'</span><span class="pun">,</span><span class="pln"> e</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">},</span><span class="pln">
    </span><span class="com">// 设置更多选项...</span><span class="pln">
</span><span class="pun">};</span><span class="pln">

</span><span class="com">// 初始化时传入选项参数</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#droppableBtn'</span><span class="pun">).</span><span class="pln">droppable</span><span class="pun">(</span><span class="pln">options</span><span class="pun">)</span></code></pre></article></section>

<script async="" src="//www.google-analytics.com/analytics.js"></script>

<section><header><h3>处理拖放过程</h3></header><article><p>通过选项可以定义多个回调函数来处理拖放过程。在一次拖放过程中，这些回调函数会按照如下顺序被调用。</p><ol>
<li><code>before</code>，在此回调函数中返回 <code>false</code>，可以取消拖动操作</li>
<li><code>start</code></li>
<li><code>drag</code>，拖动过程中会被反复执行</li>
<li><code>beforeDrop</code>，在此回调函数返回 <code>false</code>，可以取消 <code>drop</code> 的调用；</li>
<li><code>drop</code></li>
<li><code>finish</code></li>
<li><code>always</code></li>
</ol><p>当点击了鼠标但没有移动鼠标进行拖动，则只有 <code>before</code> 和 <code>always</code> 回调函数被调用。</p><h4><span class="code">before(e)</span></h4><p><code>before(e)</code> 回调函数在拖动发生之前调用，用于处理拖动前的操作或者决定取消后续的拖动事件，该回调函数参数 <code>e</code> 包含的属性定义如下：</p><ul>
<li><code>e.event</code>：鼠标按下时 <code>mousedown</code> 事件参数对象；</li>
<li><code>e.element</code>：被拖动元素。</li>
</ul><p>在该回调函数中返回 <code>false</code> 会取消这次拖动操作，后续相关事件也不会发生。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">(</span><span class="str">'#dragBtn'</span><span class="pun">).</span><span class="pln">draggable</span><span class="pun">({</span><span class="pln">
    before</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'现在不是拖动的好时机，取消这次拖动操作。'</span><span class="pun">);</span><span class="pln">

        </span><span class="com">// 通过返回 false 来取消这次拖动操作</span><span class="pln">
        </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">})</span></code></pre><h4><span class="code">start(e)</span></h4><p><code>start(e)</code> 回调函数在拖动发生前（第一次 mousemove 事件发生时）调用并且已经开始拖动时被调用。该回调函数参数 <code>e</code> 包含的属性定义同 <code>before</code> 回调函数参数。</p><h4><span class="code">drag(e)</span></h4><p><code>drag(e)</code> 回调函数在移动目标时（每一次 mousemove 事件发生时）被反复调用。该回调函数参数 <code>e</code> 包含的属性定义如下：</p><ul>
<li><code>e.event</code>：鼠标移动时 <code>mousemove</code> 事件参数对象；</li>
<li><code>e.element</code>：被拖动元素；</li>
<li><code>e.target</code>：如果现在已经被拖到一个可放置的目标元素，则该属性指示该可放置的元素，否则值为 <code>null</code>；</li>
<li><code>e.position</code>：被拖动元素当前相对父级容器（<code>container</code> 指定）的位置；</li>
<li><code>e.offset</code>：被拖动元素当前相对于开始拖动时的位置变化；</li>
<li><code>e.mouseOffset</code>：当前光标位置相对于当前相对父级容器（<code>container</code> 指定）的位置；</li>
<li><code>e.clickOffset</code>：当鼠标按下时相对于被拖动元素左上角的位置；</li>
<li><code>e.isIn</code>：指示当前被拖动位置是否已经被拖到其中一个可放置元素上；</li>
<li><code>e.isNew</code>：指示被拖动元素相对于拖动之前的位置是否被拖到一个新的可放置元素上；</li>
<li><code>e.selfTarget</code>：指示当前是否拖到自身所在的位置。</li>
</ul><p>仅当所有可放置元素都被设置了唯一的 <code>[data-id]</code> 属性，<code>isNew</code> 属性才会正常工作。</p><h4><span class="code">beforeDrop(e)</span></h4><p><code>beforeDrop(e)</code> 拖动快完成时被调用，用于干预指示此次拖动是否被拖动到有效的可放置元素上。该回调函数参数 <code>e</code> 拥有 <code>drag</code> 回调函数参数的所有属性。</p><p>在函数内返回 true 或 false 来决定是否为有效的放置。</p><h4><span class="code">drop(e)</span></h4><p><code>drop(e)</code> 拖动完成时被调用。该回调函数参数 <code>e</code> 拥有 <code>drag</code> 回调函数参数的所有属性。</p><h4><span class="code">finish(e)</span></h4><p><code>finish(e)</code> 拖动完成时被调用，此时影子元素已被移除。该回调函数参数 <code>e</code> 拥有 <code>drag</code> 回调函数参数的所有属性。</p><h4><span class="code">always(e)</span></h4><p><code>always(e)</code> 点击并弹起鼠标按钮后被调用，无论是否已经发生过拖动操作。当发生了拖动操作过程时，该回调函数参数 <code>e</code> 包含的属性定义同 <code>before</code> 回调函数参数。如果没有发生拖动操作时，该回调函数包含的属性定义如下：</p><ul>
<li><code>e.event</code>：鼠标按下时 <code>mousedown</code> 事件参数对象；</li>
<li><code>e.cancel</code>：其值为 <code>true</code>，指示是否没有发生拖动操作或者拖动操作在 <code>before</code> 中被取消。</li>
</ul><h4><span class="code">$().droppable('destroy')</span></h4><p>如果以确定不需要拖放操作，则可以调用 <code>$().droppable('destroy')</code> 来销毁拖放插件。销毁之后如果需要重新启用拖放则重新进行初始化即可。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">(</span><span class="str">'#dragDropEles'</span><span class="pun">).</span><span class="pln">droppable</span><span class="pun">(</span><span class="str">'destroy'</span><span class="pun">);</span></code></pre></article></section></div>
{/block}

{block name="script"}
<script>
$(function(){
    $('#droppableBtn').droppable({
        target: '.droppable-target',
        start: function() {
            $('#droppableContainer .droppable-target').removeClass('panel-warning').removeClass('panel-success').find('.panel-heading').text('拖动到这里吗？');
        },
        drop: function(event) {
            var msg = '真棒！';
            $('#droppableContainer .droppable-target').removeClass('panel-success').removeClass('panel-warning');
            if(event.target) {
                event.target.addClass('panel-success').find('.panel-heading').text('成功拖到目的地。');
                msg += '成功拖动到区域 ' + event.target.find('.area-name').text();
            }
            $.zui.messager.show(msg);
        },
        drag: function(event) {
            $('#droppableContainer .droppable-target').removeClass('panel-success').removeClass('panel-warning');
            if(event.target) event.target.addClass('panel-warning');
        }
    });

    $('#multiDroppableContainer').droppable({
        selector: '.btn-droppable', // 定义允许拖放的元素
        target: '.droppable-target',
        start: function() {
            $('#multiDroppableContainer .droppable-target').removeClass('panel-warning').removeClass('panel-success').find('.panel-heading').text('拖动到这里吗？');
        },
        drop: function(event) {
            var msg = '真棒！';
            $('#multiDroppableContainer .droppable-target').removeClass('panel-success').removeClass('panel-warning');
            if(event.target) {
                var elementId = event.element.find('.btn-droppable-id').text();
                event.target.addClass('panel-success').find('.panel-heading').text('成功将【按钮#' + elementId + '】拖到这里。');
                msg += '成功拖动【按钮#' + elementId + '】到区域 ' + event.target.find('.area-name').text();
            }
            $.zui.messager.show(msg);
        },
        drag: function(event) {
            $('#multiDroppableContainer .droppable-target').removeClass('panel-success').removeClass('panel-warning');
            if(event.target) event.target.addClass('panel-warning');
        }
    });
});

</script>
{/block}